var Bricabox = {
	bricaboxName: "",
	searchText:   "",
	
	// initializes the searchText value, etc.
	init: function(bricabox_name)
	{
		this.bricaboxName = bricabox_name;
		this.searchText   = "Search "+this.bricaboxName;
	},
	
	// removes "search" text and associated css class from the tab bar search field
	searchFocus: function()
	{
		var el = $('#search_field');
		if (el.val() == this.searchText)
		{
			el.val("");
			el.removeClass('grey');
		}
	},
	
	// adds "search" text and associated css class to the tab bar search field if it's blank
	searchBlur: function()
	{
		var el = $('#search_field');
		if (el.val() == "")
		{
			el.addClass('grey');
			el.val(this.searchText);
		}
	},
	
	handleSearch: function(bricabox_id)
	{
		var el = $('#search_field');
		el.keypress(function(e)
		{
			if (e.which == 13) // return/enter?
			{
				$.post('/bricabox/'+bricabox_id+'/encode_search', {value: el.val()}, function(data, textStatus)
				{
					location.href = '/entries/search/'+data;
				});
			}
		});
	},
	
	checkEntryTemplateName: function()
	{
		var el = $('#template_name_new');
		if (el.val() == "")
		{
			alert('You forgot to name your new entry template.');
			el.focus();
			return false;
		}
		else
			return true;
	}
}

var Superbar = {
	// take filter params from the superbar and turn it into a pretty url /like/this
	rewriteUrl: function()
	{
		var el 	   = $('#superbar form');
		var params = {}
		
		el.children('.superbar_field').each(function(k, v)
		{
			if (v.name == 'sort' || v.name == 'entry_type' || v.name == 'categorized')
			{
				var val = v.options[v.selectedIndex].value;
				if (v.name != 'categorized' || (v.name == 'categorized' && val > 0))
					params[v.name] = val;
			}
			else if (v.name == 'search')
			{
				if (v.value)
					params[v.name] = v.value;
			}
			else if (v.name == 'tagged')
			{
				if (v.value)
				{
					// encode whitespace and replace , with :
					params[v.name] = v.value.replace(/\s+/g, '+').replace(/,/g, ':');
				}
			}
		});
		
		var url = "/entries";
		jQuery.each(params, function(k, v)
		{
			url += "/"+k+"/"+v;
		});
		
		location.href = url;
		return false;
	}
}

var Entry = {
	lightStars: function(value, content_id)
	{
		for (i = 1; i <= value; i++)
			$('#stars_'+content_id+'_'+i).attr('src', '/assets/images/icons/star.png');
		
		if (value < 5)
			for (i = (value+1); i <= 5; i++)
				$('#stars_'+content_id+'_'+i).attr('src', '/assets/images/icons/star_dim.png');
	},
	
	showNewPanel: function()
	{
		$('#nav_add').addClass('open');
		$('#new_entry').show();
		return false;
	},
	
	hideNewPanel: function()
	{
		$('#nav_add').removeClass('open');
		$('#new_entry').hide();
		return false;
	},
	
	loadFieldsForNewEntry: function(bricabox_id, force_type_id, only_type)
	{
		type_id = force_type_id ? force_type_id : $('#content_type_id').val();
		if (type_id > 0)
			$('#new_entry_fields').load('/entry_tpl_fields', {bricabox_id: bricabox_id, type_id: type_id, only_type: only_type});
		else
			$('#new_entry_fields').attr('innerHTML', '');

		return false;
	},
	
	startEdit: function(id)
	{
		
	},
	
	endEdit: function(id)
	{
		
	},
	
	validateFields: function()
	{
		var el = $('#content_field1_value');
		if (el.val() == "")
		{
			alert("You must enter at least a title for this entry.");
			el.focus();
			return false;
		}
		else
			return true;
	},
	
	showChooseAvatar: function()
	{
		$('#entry_avatar_link').hide();
		$('#entry_avatar').show();
		$('#content_image').focus();
		
		return false;
	}
}

var EntryTpl = {
	startEdit: function(id)
	{
		$('#entry_tpl_'+id).hide();
		$('#edit_entry_tpl_'+id).show();
		$('#template_name_'+id).focus();
		
		return false;
	},
	
	endEdit: function(id)
	{
		$('#edit_entry_tpl_'+id).hide();
		$('#entry_tpl_'+id).show();
		
		return false;
	},
	
	saveTpl: function(id)
	{
		var el = $('#template_name_'+id);
		if (el.val() == "")
		{
			alert('You forgot to name your entry template.');
			el.focus();
			return false;
		}
		else
			return true;
	}
}

var Admin = {
	startEditRole: function(id)
	{
		$('#role_for_'+id).hide();
		$('#edit_role_for_'+id).show();
		$('#username_or_email').focus();
		
		return false;
	},
	
	endEditRole: function(id)
	{
		$('#edit_role_for_'+id).hide();
		$('#role_for_'+id).show();
		
		return false;
	},
	
	checkAddMember: function()
	{
		var el = $('#username_or_email');
		if (el.val() == "")
		{
			alert('Please enter the username or email address of the person you\'d like to make a member.');
			el.focus();
			return false;
		}
		else
			return true;
	},
	
	chooseTheme: function(theme_id, current_id)
	{
		$('#theme_'+current_id).removeClass('current');
		$('#theme_'+theme_id).addClass('current');
	}
}

var Userpanel = {
	open: function()
	{
		var el = $('#user_panel');
		if (el.css('display') == 'none')
		{
			$('#user_panel').show();
			$('#user_panel_link').addClass('open');
		}
		else if (el.css('display') == 'block')
		{
			this.hide();
		}
		return false;
	},
	
	hide: function()
	{
		$('#user_panel_link').removeClass('open');
		$('#user_panel').hide();
		return false;
	}
}

var TagsBlock = {
	saveTag: function(instance_id, content_id, bricabox_id)
	{
		var el = $('#tag_name_'+instance_id);
		
		params = {
			instance_id: instance_id,
			content_id:  content_id,
			value: 		 el.val(),
			_action: 	 'save_tags'
		};
		$('#tags_'+instance_id).load('/bricabox/'+bricabox_id+'/block_proxy', params);
		
		return false;
	},
	
	deleteTag: function(instance_id, id, value, content_id, bricabox_id)
	{
		if (confirm('Are you sure you want to remove the tag "'+value+'" from this entry?'))
		{
			params = {
				instance_id: instance_id,
				tag_id:		 id,
				value: 		 value,
				content_id:  content_id,
				_action: 	 'remove_tag'
			};
			$('#tags_'+instance_id).load('/bricabox/'+bricabox_id+'/block_proxy', params);
		}
		return false;
	}
}

var PrefPane = {
	toggle: function(id)
	{
		$('#block_prefs_'+id).toggle();
		return false;
	}
}

var Ctgy = {
	changeCat: function(content_id, instance_id, bricabox_id)
	{
		var el = $('#category_for_'+content_id);
		var value = el.val();
		params 	  = {
			instance_id: instance_id,
			value: 		 value,
			content_id:  content_id,
			_action: 	 'change_ctgy'
		};
		$('#cat_link_'+instance_id).load('/bricabox/'+bricabox_id+'/block_proxy', params);
	},
	
	removeCat: function(category_id, instance_id, bricabox_id)
	{
		if (confirm('Are you sure you want to remove this category?'))
		{
			var el = $('#ctgy_'+category_id);
			el.hide();
			params 	  = {
				instance_id: instance_id,
				value: 		 category_id,
				_action: 	 'rem_ctgy'
			};
			$.post('/bricabox/'+bricabox_id+'/block_proxy', params);
		}
		return false;
	},
	
	addCat: function(instance_id, bricabox_id)
	{
		var el 	  = $('#ctgy_name_'+instance_id);
		var value = el.val();
		params 	  = {
			instance_id: instance_id,
			value: 		 value,
			_action: 	 'add_ctgy'
		};
		$.post('/bricabox/'+bricabox_id+'/block_proxy', params, function(a,b)
		{
			location.reload();
		});
	}
}